#include "methodsOptionsPanel.h"
#include "defines/methods.h"

MethodsOptionsPanel::MethodsOptionsPanel(QWidget *_parent) : QWidget(_parent){
    implicitJacobiThreshold = 0.001;
    implicitJacobiMaxIteration = 100;

    implicitSORThreshold = 0.2;
    implicitSORRelaxation = 1;
    implicitSORMaxIteration = 100;

    improvedVerletEta = 0.01;

    QVBoxLayout *mainLayout = new QVBoxLayout;

    setLayout(mainLayout);
}

void remove(QLayout* layout)
{
    QLayoutItem* child;
    while((child = layout->takeAt(0)) != 0)
    {
        if(child->layout() != 0)
        {
            remove(child->layout());
        }
        else if(child->widget() != 0)
        {
            delete child->widget();
        }

        delete child;
    }
}

void MethodsOptionsPanel::changeLayout(unsigned int _method){

    QLayout *oldLayout = layout();
    remove(oldLayout);
    delete oldLayout;


    switch(_method)
    {
        case FORWARD_EULER:
        case MIDPOINT:
        case RUNGE_KUTTA:
        case ADAPTIVE_RUNGE_KUTTA:
        case APROXIMATED_IMPLICIT:
        case HYBRID_INTEGRATION:
        case APROXIMATED_JACOBI:
        {
            QVBoxLayout *mainLayout = new QVBoxLayout;

            setLayout(mainLayout);
            break;

        }
        case IMPROVED_VERLET:
        {

            //------------------------------------improvedVerletEta
            QLabel *improvedVerletEtaLabel = new QLabel(QString::fromUtf8("η:"));

            QDoubleSpinBox *improvedVerletEtaDoubleSpinBox = new QDoubleSpinBox();
            improvedVerletEtaDoubleSpinBox->setRange(0.0, 50.0);
            improvedVerletEtaDoubleSpinBox->setSingleStep(0.001);
            improvedVerletEtaDoubleSpinBox->setDecimals(5);
            improvedVerletEtaDoubleSpinBox->setValue(improvedVerletEta);
            connect(improvedVerletEtaDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeImprovedVerletEta(double)));

            QHBoxLayout *improvedVerletEtaLayout = new QHBoxLayout;
            improvedVerletEtaLayout->addWidget(improvedVerletEtaLabel);
            improvedVerletEtaLayout->addWidget(improvedVerletEtaDoubleSpinBox);

            QVBoxLayout *mainLayout = new QVBoxLayout;
            mainLayout->addLayout(improvedVerletEtaLayout);
            mainLayout->addStretch(1);

            setLayout(mainLayout);
            break;
        }
        case IMPLICIT_SOR:
        {
            //------------------------------------implicitSORThreshold
            QLabel *implicitSORThresholdLabel = new QLabel(QString::fromUtf8("Threshold:"));

            QDoubleSpinBox *implicitSORThresholdDoubleSpinBox = new QDoubleSpinBox();
            implicitSORThresholdDoubleSpinBox->setRange(0.0, 50.0);
            implicitSORThresholdDoubleSpinBox->setSingleStep(0.001);
            implicitSORThresholdDoubleSpinBox->setDecimals(5);
            implicitSORThresholdDoubleSpinBox->setValue(implicitSORThreshold);
            connect(implicitSORThresholdDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeImplicitSORThreshold(double)));

            QHBoxLayout *implicitSORThresholdLayout = new QHBoxLayout;
            implicitSORThresholdLayout->addWidget(implicitSORThresholdLabel);
            implicitSORThresholdLayout->addWidget(implicitSORThresholdDoubleSpinBox);

            //------------------------------------implicitSORRelaxation
            QLabel *implicitSORRelaxationLabel = new QLabel(QString::fromUtf8("Relaxamento:"));

            QDoubleSpinBox *implicitSORRelaxationDoubleSpinBox = new QDoubleSpinBox();
            implicitSORRelaxationDoubleSpinBox->setRange(0.0, 50.0);
            implicitSORRelaxationDoubleSpinBox->setSingleStep(0.001);
            implicitSORRelaxationDoubleSpinBox->setDecimals(5);
            implicitSORRelaxationDoubleSpinBox->setValue(implicitSORRelaxation);
            connect(implicitSORRelaxationDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeImplicitSORRelaxation(double)));

            QHBoxLayout *implicitSORRelaxationLayout = new QHBoxLayout;
            implicitSORRelaxationLayout->addWidget(implicitSORRelaxationLabel);
            implicitSORRelaxationLayout->addWidget(implicitSORRelaxationDoubleSpinBox);


            //------------------------------------implicitSORMaxIteration
            QLabel *implicitSORMaxIterationLabel = new QLabel(QString::fromUtf8("Máximo de interações:"));

            QSpinBox *implicitSORMaxIterationSpinBox = new QSpinBox();
            implicitSORMaxIterationSpinBox->setRange(1, 10000);
            implicitSORMaxIterationSpinBox->setSingleStep(100);
            implicitSORMaxIterationSpinBox->setValue(implicitSORMaxIteration);
            connect(implicitSORMaxIterationSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeImplicitSORMaxIteration(int)));

            QHBoxLayout *implicitSORMaxIterationLayout = new QHBoxLayout;
            implicitSORMaxIterationLayout->addWidget(implicitSORMaxIterationLabel);
            implicitSORMaxIterationLayout->addWidget(implicitSORMaxIterationSpinBox);


            QVBoxLayout *mainLayout = new QVBoxLayout;
            mainLayout->addLayout(implicitSORThresholdLayout);
            mainLayout->addLayout(implicitSORRelaxationLayout);
            mainLayout->addLayout(implicitSORMaxIterationLayout);
            mainLayout->addStretch(1);

            setLayout(mainLayout);
            break;
        }
        case IMPLICIT_JACOBI:
        {
            //------------------------------------implicitJacobiThreshold
            QLabel *implicitJacobiThresholdLabel = new QLabel(QString::fromUtf8("Threshold:"));

            QDoubleSpinBox *implicitJacobiThresholdDoubleSpinBox = new QDoubleSpinBox();
            implicitJacobiThresholdDoubleSpinBox->setRange(0.0, 50.0);
            implicitJacobiThresholdDoubleSpinBox->setSingleStep(0.001);
            implicitJacobiThresholdDoubleSpinBox->setDecimals(5);
            implicitJacobiThresholdDoubleSpinBox->setValue(implicitJacobiThreshold);
            connect(implicitJacobiThresholdDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeImplicitJacobiThreshold(double)));

            QHBoxLayout *implicitJacobiThresholdLayout = new QHBoxLayout;
            implicitJacobiThresholdLayout->addWidget(implicitJacobiThresholdLabel);
            implicitJacobiThresholdLayout->addWidget(implicitJacobiThresholdDoubleSpinBox);


            //------------------------------------implicitJacobiMaxIteration
            QLabel *implicitJacobiMaxIterationLabel = new QLabel(QString::fromUtf8("Máximo de interações:"));

            QSpinBox *implicitJacobiMaxIterationSpinBox = new QSpinBox();
            implicitJacobiMaxIterationSpinBox->setRange(1, 10000);
            implicitJacobiMaxIterationSpinBox->setSingleStep(100);
            implicitJacobiMaxIterationSpinBox->setValue(implicitJacobiMaxIteration);
            connect(implicitJacobiMaxIterationSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeImplicitJacobiMaxIteration(int)));

            QHBoxLayout *implicitJacobiMaxIterationLayout = new QHBoxLayout;
            implicitJacobiMaxIterationLayout->addWidget(implicitJacobiMaxIterationLabel);
            implicitJacobiMaxIterationLayout->addWidget(implicitJacobiMaxIterationSpinBox);


            QVBoxLayout *mainLayout = new QVBoxLayout;
            mainLayout->addLayout(implicitJacobiThresholdLayout);
            mainLayout->addLayout(implicitJacobiMaxIterationLayout);
            mainLayout->addStretch(1);

            setLayout(mainLayout);
            break;
        }
    }
}
